package me.zhengjie.modules.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import me.zhengjie.base.Code;
import me.zhengjie.enums.DeletedEnum;
import me.zhengjie.modules.system.converter.MachineConverter;
import me.zhengjie.modules.system.domain.entity.SysUserBankCard;
import me.zhengjie.modules.system.domain.req.SysUserBankCardReqDTO;
import me.zhengjie.modules.system.domain.req.SysUserBankCardReqQueryDTO;
import me.zhengjie.modules.system.domain.rsp.SysUserBankCardRspDTO;
import me.zhengjie.modules.system.mapper.SysUserBankCardMapper;
import me.zhengjie.modules.system.service.SysUserBankCardService;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@RequiredArgsConstructor
public class SysUserBankCardServiceImpl extends ServiceImpl<SysUserBankCardMapper, SysUserBankCard> implements
        SysUserBankCardService {


    /**
     * 添加
     *
     * @param reqDTO
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean save(SysUserBankCardReqDTO reqDTO) {
        SysUserBankCard sysUserBankCard = MachineConverter.INSTANCE.sysUserBankCardReqDTOTo(reqDTO);
        return super.save(sysUserBankCard);
    }

    /**
     * 修改
     *
     * @param reqDTO
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(SysUserBankCardReqDTO reqDTO) {
        SysUserBankCard sysUserBankCard = MachineConverter.INSTANCE.sysUserBankCardReqDTOTo(reqDTO);
        return super.updateById(sysUserBankCard);
    }


    /**
     * 删除
     *
     * @param code
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(Code code) {
        return super.removeById(code.getCode());
    }

    /**
     * 主键查询
     *
     * @param code
     */
    public SysUserBankCardRspDTO findById(Code code) {
        SysUserBankCard sysUserBankCard = lambdaQuery().eq(SysUserBankCard::getId, code.getCode())
                .eq(SysUserBankCard::getIsDel, DeletedEnum.YES.getCode())
                .one();
        return MachineConverter.INSTANCE.sysUserBankCardToRsp(sysUserBankCard);
    }

    /**
     * 列表
     *
     * @param reqQueryDTO
     * @return
     */
    public PageResult<SysUserBankCardRspDTO> findAll(SysUserBankCardReqQueryDTO reqQueryDTO) {
        PageHelper.startPage(reqQueryDTO.getPageNo(), reqQueryDTO.getPageSize(), true);
        List<SysUserBankCardRspDTO> list = getBaseMapper().findAll(reqQueryDTO);
        PageInfo<SysUserBankCardRspDTO> pageInfo = new PageInfo<>(list);
        return PageUtil.toPage(pageInfo.getList(), pageInfo.getTotal());
    }

}
